home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
367_01
/
futi14as.zoo
/
tail.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-22
|
26KB
|
1,027 lines
/* tail -- output last part of file(s)
Copyright (C) 1989, 1990 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
This port is also distributed under the terms of the
GNU General Public License as published by the
Free Software Foundation.
Please note that this file is not identical to the
original GNU release, you should have received this
code as patch to the official release. */
#ifdef MSDOS
static char RCS_Id[] =
"$Header: e:/gnu/fileutil/RCS/tail.c 1.4.0.4 90/09/19 12:09:20 tho Exp $";
static char Program_Id[] = "tail";
static char RCS_Revision[] = "$Revision: 1.4.0.4 $";
#define VERSION \
"GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
(sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
#define COPYING \
"This is free software, distributed under the terms of the\n" \
"GNU General Public License. For details, see the file COPYING.\n"
#endif /* MSDOS */
/* Can display any amount of data, unlike the Unix version, which uses
a fixed size buffer and therefore can only deliver a limited number
of lines.
Usage: tail [-b [+]#] [-c [+]#] [-n [+]#] [-fqv] [+blocks [+]#]
[+bytes [+]#] [+lines [+]#] [+follow] [+quiet] [+silent]
[+verbose] [file...]
tail [+/-#bcflqv] [file...]
Options:
-b, +blocks # Tail by # 512-byte blocks.
-c, +bytes # Tail by # bytes.
-f, +follow Loop forever trying to read more characters at the
end of the file, on the assumption that the file
is growing. Ignored if reading from a pipe.
Cannot be used if more than one file is given.
-l, -n, +lines # Tail by # lines.
-q, +quiet, +silent Never print filename headers.
-v, +verbose Always print filename headers.
If a number (#) starts with a `+', begin printing with the #th item
from the start of each file, instead of from the end.
Reads from standard input if no files are given or when a filename of
``-'' is encountered.
By default, filename headers are printed only more than one file
is given.
By default, prints the last 10 lines (tail -n 10).
Started by Paul Rubin <phr@ai.mit.edu>
Finished by David MacKenzie <djm@ai.mit.edu> */
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <sys/types.h>
#include "system.h"
#ifdef STDC_HEADERS
#include <errno.h>
#include <stdlib.h>
#define ISDIGIT(c) (isdigit ((unsigned char) (c)))
#else
#define ISDIGIT(c) (isascii (c) && isdigit (c))
char *malloc ();
void free ();
extern int errno;
#endif
#ifndef _POSIX_SOURCE
long lseek();
#endif
/* Number of items to tail. */
#define DEFAULT_NUMBER 10
/* The number of bytes in a block (-b option). */
#define BLOCKSIZE 512
/* Size of atomic reads. */
#define BUFSIZE (BLOCKSIZE * 8)
/* Masks for the operation mode. If neither BYTES nor BLOCKS is set,
tail operates by lines. */
#define BYTES 1 /* Tail by characters. */
#define BLOCKS 2 /* Tail by blocks. */
#define FOREVER 4 /* Read from end of file forever. */
#define START 8 /* Count from start of file instead of end. */
#define HEADERS 16 /* Print filename headers. */
#ifdef MSDOS
#define BINARY 32 /* Suppress crlf translation. */
#endif
/* When to print the filename banners. */
enum header_mode
{
multiple_files, always, never
};
#ifdef MSDOS
#include <io.h>
#include <string.h>
extern void main (int, char **);
extern void write_header (char *);
extern int tail (char *, int, int, long);
extern int tail_file (char *, int, long);
extern int tail_bytes (char *, int, int, long);
extern int tail_lines (char *, int, int, long);
extern int file_lines (char *, int, long, long);
extern int pipe_lines (char *, int, long);
extern int pipe_bytes (char *, int, long);
extern int start_bytes (char *, int, long);
extern int start_lines (char *, int, long);
extern void dump_remainder (char *, int, int);
extern void xwrite (int, char *, int);
extern char *xmalloc (int);
extern long atou (char *);
extern char *basename (char *);
extern void error (int status, int errnum, char *message, ...);
extern void usage (void);
#else /* not MSDOS */
char *xmalloc ();
int file_lines ();
int pipe_bytes ();
int pipe_lines ();
int start_bytes ();
int start_lines ();
int tail ();
int tail_bytes ();
int tail_file ();
int tail_lines ();
long atou();
void dump_remainder ();
void error ();
void usage ();
void write_header ();
void xwrite ();
#endif /* not MSDOS */
/* The name this program was run with. */
char *program_name;
struct option long_options[] =
{
#ifdef MSDOS
{"binary", 1, NULL, 'B'},
{"copying", 0, NULL, 30},
{"version", 0, NULL, 31},
#endif
{"blocks", 1, NULL, 'b'},
{"bytes", 1, NULL, 'c'},
{"follow", 0, NULL, 'f'},
{"lines", 1, NULL, 'n'},
{"quiet", 0, NULL, 'q'},
{"silent", 0, NULL, 'q'},
{"verbose", 0, NULL, 'v'},
{NULL, 0, NULL, 0}
};
void
main (argc, argv)
int argc;
char **argv;
{
enum header_mode header_mode = multiple_files;
int errors = 0; /* Exit status. */
int mode = 0; /* Flags. */
/* In START mode, the number of items to skip before printing; otherwise,
the number of items at the end of the file to print. Initially, -1
means the value has not been set. */
long number = -1;
int c; /* Option character. */
int longind; /* Index in `long_options' of option found. */
program_name = argv[0];
if (argc > 1
&& ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
|| (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
{
/* Old option syntax: a dash or plus, one or more digits (zero digits
are acceptable with a plus), and one or more option letters. */
if (argv[1][0] == '+')
mode |= START;
if (argv[1][1] != 0)
{
for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
number = number * 10 + *argv[1] - '0';
/* Parse any appended option letters. */
while (*argv[1])
{
switch (*argv[1])
{
case 'b':
mode |= BLOCKS;
mode &= ~BYTES;
break;
case 'c':
mode |= BYTES;
mode &= ~BLOCKS;
break;
case 'f':
mode |= FOREVER;
break;
case 'l':
mode &= ~(BYTES | BLOCKS);
break;
case 'q':
header_mode = never;
break;
case 'v':
header_mode = always;
break;
default:
error (0, 0, "unrecognized option `-%c'", *argv[1]);
usage ();
}
++argv[1];
}
}
/* Make the options we just parsed invisible to getopt. */
argv[1] = argv[0];
argv++;
argc--;
}
#ifdef MSDOS
while ((c = getopt_long (argc, argv, "b:c:n:fqvB", long_options, &longind))
#else
while ((c = getopt_long (argc, argv, "b:c:n:fqv", long_options, &longind))
#endif
!= EOF)
{
switch (c)
{
#ifdef MSDOS
case 30:
fprintf (stderr, COPYING);
exit (0);
break;
case 31:
fprintf (stderr, VERSION);
exit (0);
break;
case 'B':
mode |= BINARY;
break;
#endif
case 'b':
mode |= BLOCKS;
mode &= ~BYTES;
if (*optarg == '+')
{
mode |= START;
++optarg;
}
else if (*optarg == '-')
++optarg;
number = atou (optarg);
if (number == -1)
error (1, 0, "invalid number `%s'",